package algebra.tests;
import junit.framework.TestCase;
import algebra.Algebra;


public class AlgebraTest extends TestCase {

	private static void printMatrix(float m[][]) {

		int row;
		for(row=0; row < m.length; row++) {
			printVector(m[row]);
		}
	}

	private static void printVector(float v[]) {
		int col;
		for(col = 0; col < v.length; col++) {
			System.out.printf("[ %-3.2f ]", v[col]);
		}
		System.out.println("");
	}

	public void testInverse() {
		System.out.println("*");
		System.out.println("* testInverse");
		System.out.println("*");

		float	m[][] = new float[][] {
			{	2.0f,	3.0f,	1.0f	},
			{	1.0f,	-2.0f,	-1.0f	},
			{	2.0f,	0.0f,	-1.0f	}
		};
		float	mi[][];
		float	mi2[][];

		System.out.println("-------------------------");
		printMatrix(m);
		System.out.println("-------------------------");

		mi = Algebra.inverse(m);

		System.out.println("-------------------------");
		printMatrix(mi);
		System.out.println("-------------------------");

		mi2 = Algebra.inverse(mi);

		System.out.println("-------------------------");
		printMatrix(mi2);
		System.out.println("-------------------------");

		assertTrue(Algebra.isEqual(mi2,m));

	}

	public void testGaussian() {
		System.out.println("*");
		System.out.println("* testGaussian");
		System.out.println("*");

		float m[][] = new float[][] {
			{ 2.0f, 0.0f, 0.0f, 2.0f } ,
			{ 0.0f, 2.0f, 0.0f, 4.0f } ,
			{ 0.0f, 0.0f, 2.0f, 6.0f }
		};

		float	v[] = null;

		System.out.println("-------------------------");
		printMatrix(m);
		System.out.println("-------------------------");

		m = Algebra.upperTriangular(m);

		System.out.println("-------------------------");
		printMatrix(m);
		System.out.println("-------------------------");

		assertTrue(m!=null);
		assertTrue(m[0][0] == 1f);
		assertTrue(m[1][0] == 0f);
		assertTrue(m[2][0] == 0f);
		assertTrue(m[1][1] == 1f);
		assertTrue(m[2][1] == 0f);
		assertTrue(m[2][2] == 1f);

		v = Algebra.solve(m,Algebra.numRows(m));

		System.out.println("-------------------------");
		printVector(v);
		System.out.println("-------------------------");

		assertTrue(v[0]==1f);
		assertTrue(v[1]==2f);
		assertTrue(v[2]==3f);

	}


}
